<?php
/**
 * Created by PhpStorm.
 * User: chenxiaogang
 * Date: 2017/11/10
 * Time: 下午2:08
 */

namespace User\Model;


use Common\Model\CommonModel;

class Log extends CommonModel
{
    protected $connection = 'DB_CONFIG2';
    protected $trueTableName = 'system_log';

    /**
     * @use          [获取用户历史上的最大的一次逾期费用]
     * @author       chenxiaogang
     * @param $user_id
     * @return mixed
     */
    public function getMaxOverdueAmt($user_id)
    {
        $where = '`status`=1 and user_id in(select id from orders where user_id=' . $user_id . ') and types=5';
        $data = $this->field(['max(amount)'=>'max_overdue'])->where( $where )->find();
        return $data['max_overdue'];
    }

    /**
     * @use          [获取用户的累计逾期次数和累计逾期金额]
     * @author       chenxiaogang
     * @param $user_id
     * @return array
     */
    public function getOverdue($user_id){
        $where = '`status`=1 and user_id in(select id from orders where user_id=' . $user_id . ') and types=5';
        $data = $this->field(['count(id)'=>'over_due_count','sum(amount)'=>'over_due_amount'])->where( $where )->find();
        return $data;
    }

    /**
     * @use          [查询用户的大于7天、30天、90天分别的逾期次数、逾期金额]
     * @author       chenxiaogang
     * @param $user_id
     * @return array
     */
    public function getDaysOverdue($user_id){
        $where = '`status`=1 and user_id in(select id from orders where user_id=' . $user_id . ') and types=5';
        $data = $this->field([
            'count(if(amount>=150*7,true,null))'=>'overdue_7days_cnt',
            'sum(if(amount>150*7,amount,0.00))'=>'overdue_7days_amt',
            'count(if(amount>=150*30,true,null))'=>'overdue_30days_cnt',
            'sum(if(amount>150*30,amount,0.00))'=>'overdue_30days_amt',
            'count(if(amount>=150*90,true,null))'=>'overdue_90days_cnt',
            'sum(if(amount>150*90,amount,0.00))'=>'overdue_90days_amt',
        ])->where($where)->find();
        return $data;
    }

}